package com.hex.liyue.usermodule.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.hex.liyue.R;
import com.hex.liyue.commonmodule.activity.BaseActivity;
import com.hex.liyue.commonmodule.utils.L;
import com.hex.liyue.commonmodule.utils.StatusBarCompatUtils;
import com.hex.liyue.commonmodule.view.recycleview.YRecycleview;
import com.hex.liyue.usermodule.adapter.AdapterFriends;
import com.hex.liyue.usermodule.bean.MyFriendsResultBean;
import com.hex.liyue.usermodule.bean.MyDownLinesResultBean;
import com.hex.liyue.usermodule.presenter.IMoreFriendsView;
import com.hex.liyue.usermodule.presenter.MoreFriendsPresenter;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * Created by $cmm on 2016/6/21.
 * <p/>
 * 我的下线小伙伴
 */
public class MoreFriendsActivity extends BaseActivity implements IMoreFriendsView, YRecycleview.RefreshAndLoadMoreListener {
    @Bind(R.id.iv_toobar_back)
    ImageView ivToobarBack;
    @Bind(R.id.tv_toobar_title)
    TextView tvToobarTitle;
    @Bind(R.id.rv_morefriends)
    YRecycleview rvMoreFriends;

    private MoreFriendsPresenter moreFriendsPresenter;
    private AdapterFriends adapterFriends;
    private List<MyFriendsResultBean.DataBean.MatesBean> list = new ArrayList<>();
    private int page = 1;
    private int page_num = 10;

    @Override
    protected void bindPresenter() {
        moreFriendsPresenter = new MoreFriendsPresenter(this);
    }

    @Override
    protected void initData(Bundle extras) {
        adapterFriends = new AdapterFriends();
        rvMoreFriends.setLayoutManager(new LinearLayoutManager(this));
        rvMoreFriends.setAdapter(adapterFriends);
        rvMoreFriends.setRefreshAndLoadMoreListener(this);

        moreFriendsPresenter.myFrendis(page, page_num);
    }

    @Override
    public int getContentLayout() {
        return R.layout.activity_morefriends;
    }

    @Override
    protected void initToolbar() {
        StatusBarCompatUtils.compat(this);
        ivToobarBack.setVisibility(View.VISIBLE);
        tvToobarTitle.setText("我的下线小伙伴");
    }

    @OnClick({R.id.iv_toobar_back})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_toobar_back:
                onBackPressed();
                break;
        }
    }


    @Override
    public void setAdapter(MyDownLinesResultBean myDownLinesResultBean) {
        if (page == 1) {
            list.clear();
        }
        if (myDownLinesResultBean != null) {
            if (myDownLinesResultBean.data.size() == 0 && page > 1) {
                page--;
            }
            for (MyFriendsResultBean.DataBean.MatesBean matesBean : myDownLinesResultBean.data) {
                list.add(matesBean);
            }
        }

        adapterFriends.setList(list);
        adapterFriends.notifyDataSetChanged();
        rvMoreFriends.reSetStatus();
        L.d("WJ", page + "=size=" + myDownLinesResultBean.data.size());
    }

    @Override
    public void showLod(String msg) {
        showLoad(this, msg);
    }

    @Override
    public void closeLod() {
        closeLoad();
    }

    @Override
    public void showT(String msg) {
        showToast(this, msg);
    }

    @Override
    public void onRefresh() {
        page = 1;
        moreFriendsPresenter.myFrendis(page, page_num);
    }

    @Override
    public void onLoadMore() {
        page++;
        moreFriendsPresenter.myFrendis(page, page_num);
    }
}
